package com.igbeok.miscellaneous.hibernate;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.igbeok.miscellaneous.hibernate.entity.User;

public class Client {
	public static void main(String[] args) {
		Configuration cfg = new Configuration().configure();// 读取hibernate.cfg.xml
		SessionFactory sessionFactory = cfg.buildSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			session.beginTransaction();
			User user = new User();
			user.setCreateTime(new Date());
			user.setExpireTime(new Date());
			user.setName("rico");
			session.save(user);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}
	}
}
